<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 头部 查询商品与添加商品 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <div>
            <el-input placeholder="请输入内容" v-model="queryData.query" clearable  @clear="search" class="input-with-select"  @keyup.native.enter ="search" >
              <el-button slot="append" icon="el-icon-search"  @click="search"></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="14">
          <el-button type="primary" @click="skipGoodsAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品表格 -->
      <el-table
        :data="goodsListData"
        border
        stripe
        style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="名称" ></el-table-column>
        <el-table-column prop="goods_price" label="价格" width="100"></el-table-column>
        <el-table-column prop="goods_weight" label="重量" width="100"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150">
          <!-- 由于返回的时间是秒数 需要加过滤(一般在全局-main.js) -->
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit"  size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete"  size="mini" @click="delGoos(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 页码数据
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品总数据
      total: 0,
      // 商品列表数据
      goodsListData: []
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取商品总数列表
    async getGoodsList () {
      const { data: res } = await this.$axios.get('goods', {
        params: this.queryData
      })
      this.goodsListData = res.data.goods
      this.total = res.data.total
    },
    // 分页 每页条数发生变化时
    handleSizeChange (pagesize) {
      this.queryData.pagesize = pagesize
      this.getGoodsList()
    },
    // 分页 页数变化事件
    handleCurrentChange (pagenum) {
      this.queryData.pagenum = pagenum
      this.getGoodsList()
    },
    // 搜索商品
    search () {
      this.getGoodsList()
    },
    // 删除商品
    delGoos (goodsId) {
      this.$messageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete(`goods/${goodsId}`)
        if (res.meta.status !== 200) return this.message(res.meta.status)
        this.$message.success('删除成功!')
        this.getGoodsList()
      }).catch(() => {
        this.$message('已取消删除')
      })
    },
    // 跳转app
    skipGoodsAdd () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style scoped>

</style>
